
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Function.h"
#include	"Power.h"
#include	"Model_Option.h"
#include	"USB.h"
#include	"AUDIO.h"
#ifdef  LC786XXX
#include	"LC786XXX.h"
#endif
#ifdef  BLUETOOTH
#include	"Bluetooth.h"
#endif
#ifdef  HERO
#include	"Hero_Drv.h"
#endif
#ifdef  CD
#include	"CD_MECHA.h"
#include	"CD.h"
#endif
#include	"Utill.h"
#ifdef  ATOMIC
#include	"Atomic_DRV.h"
#endif

extern unsigned char ucTask_Timer5msec;
#ifdef  ST7565P
extern unsigned char ucST7565P_Seq;
#endif
#ifdef  HERO
extern unsigned char ucHERO_BootSeq;
#endif
#ifdef  ATOMIC
extern unsigned char ucATOMIC_BootSeq;
#endif
extern unsigned char ucFUNC_FunctionSeq;
extern unsigned char ucUSB_Scan;
extern unsigned char ucFUNC_Power;
#ifdef  CD
extern unsigned char ucDECK_Mode;
extern unsigned char ucDECK_Disc;
extern void DECK_SetIdle(void);
#endif
extern UI_FUNCTION ucFUNC_Current;

extern void Device_InitializeStart(void);
extern unsigned char FUNC_CheckFunctionAvailable(UI_FUNCTION ucFunc);
extern void FUNC_FunctionStop(void);
extern void USB_PlayModeInit(void);

unsigned char ucPWR_AccStatus;
unsigned char ucPWR_AccPortLast;
unsigned char ucPWR_WakeUpMode;
unsigned char ucPWR_AntennaShorted;
unsigned char ucPWR_AntennaPortLast;
unsigned char ucPWR_A2DP_Resume;
unsigned char ucPWR_AntennaActive;

unsigned char ucPWR_BackUp_Timer5msec;
#define	PWR_BACKUP_DET_TIME		100/5
unsigned char ucPWR_BackUpPortLast;
unsigned char ucPWR_BackUpStatus;
unsigned char ucPWR_AccMute_Timer5msec;
unsigned char ucPWR_Antenna_Timer50msec;

#ifdef  BLUETOOTH
unsigned char ucPWR_BT_ConnectResume;

extern unsigned char ucBT_AutoConnection;
extern void BT_ProcessInit(void);
extern unsigned char BT_ConnectStatus(void);
extern unsigned char BT_A2DP_ConnectStatus(void);
extern unsigned char BT_FunctionStop(void);
#endif
#ifdef  PT6524
extern void PT6524_DisplayOff(void);
#endif
#ifdef  ST7565P
extern void ST7565P_DisplayOff(void);
#endif

#ifdef	LC786XXX
extern void LC786XXX_StopProcess(void);
extern unsigned char ucLC786XXX_InitializeComplete;
extern void LC786XXX_MediaStop(UI_FUNCTION ucFunc);
#endif

extern void AUDIO_MuteRelease(void);
extern unsigned short CPU_ADC_GetBatteryLevel(void);
extern unsigned short usCPU_ADC_Battery;

#define	BATTERY_12V		(1023 * 1.38) / 3.3
#define	BATTERY_13V		(1023 * 1.48) / 3.3
#define	BATTERY_18V		639//(1023 * 1.38) / 3.3 // Mute

#define	BAT_SLEEP_LEV	BATTERY_12V
#define	BAT_WAKEUP_LEV	BATTERY_18V

void PWR_SetWakeUpMode(unsigned char ucMode)
{
	ucPWR_WakeUpMode = ucPWR_WakeUpMode | ucMode;
}

void PWR_SetAntennaActive(unsigned char ucMode)
{
	if  (ucMode == 0x01)
	{
		ucPWR_AntennaActive = 1;
		ucPWR_Antenna_Timer50msec = 500/50;
	}
	else
	{
		ucPWR_AntennaActive = 0;
	}
}

void PWR_PowerControl(unsigned char ucPwr, unsigned char ucDes)
{
#ifdef  LV56801P
	if  (ucDes == 1)		// On
	{
		if  ((ucPwr & PWR_CTR2) == PWR_CTR2)
		{
	//		if  (ucPWR_AntennaShorted == 0)
			{
				PO_PWR2 = 1;
			}
		}
		if  ((ucPwr & PWR_USB) == PWR_USB)
		{
			PO_PWR3 = 1;
		}
		if  ((ucPwr & PWR_CTR1) == PWR_CTR1)
		{
			PO_PWR1 = 1;
		}
		if  ((ucPwr & PWR_BACKLIGHT) == PWR_BACKLIGHT)
		{
			PO_BACKLIGHT = 1;
		}
		if  ((ucPwr & PWR_ANT) == PWR_ANT)
		{
			PO_ANT = 1;
		}
	}
	else
	{
		if  ((ucPwr & PWR_CTR2) == PWR_CTR2)
		{
	//		if  (ucPWR_AntennaShorted == 0)
			{
				PO_PWR2 = 0;
			}
		}
		if  ((ucPwr & PWR_ANT) == PWR_ANT)
		{
			PO_PWR2 = 0;
		}
		if  ((ucPwr & PWR_USB) == PWR_USB)
		{
			PO_PWR3 = 0;
		}
		if  ((ucPwr & PWR_CTR1) == PWR_CTR1)
		{
			PO_PWR1 = 0;
		}
		if  ((ucPwr & PWR_BACKLIGHT) == PWR_BACKLIGHT)
		{
			PO_BACKLIGHT = 0;
		}
		if  ((ucPwr & PWR_ANT) == PWR_ANT)
		{
			PO_ANT = 0;
		}
	}
#endif

}


unsigned short usPWR_AccSequence_Timer5msec;
unsigned char ucPWR_ACC_Sequence;

unsigned char PRW_AskACCStatusIdle(void)
{
	if  (ucPWR_ACC_Sequence == PWR_ACC_IDLE)
	{
		return(0);
	}
	return(1);
}

void PWR_CheckAntennaStatus(void)	// High is Short ( abnormal )
{
#if  0
	unsigned char ucAntPort;

	if  (ucPWR_AntennaActive == 0)
	{
		ucPWR_AntennaShorted = 0;
		return;
	}		

	if  (ucPWR_Antenna_Timer50msec != 0)
	{
		return;
	}

	ucAntPort = PI_ANT;

	if  (ucAntPort == ucPWR_AntennaPortLast)
	{
		if  (ucAntPort == 0)
		{
			if  (ucPWR_AntennaShorted == 0)
			{
				PWR_PowerControl(PWR_ANT, 0);
			}
			ucPWR_AntennaShorted = 1;
		}
		else
		{
			ucPWR_AntennaShorted = 0;
		}
	}
	ucPWR_AntennaPortLast = ucAntPort;
#endif
}

void PWR_GetACCPort(void)
{
	unsigned char ucAccPort;

	ucAccPort = PI_ACC;

	if  (ucPWR_AccPortLast == ucAccPort)
	{
		if  (ucAccPort == 1)
		{
		#ifdef  DEBUG_UART
			if  (ucPWR_AccStatus == 0)
			{
				LOG_ACC("PWR_GetACCPort:ON");
			}
		#endif
			ucPWR_AccStatus = 1;
		}
		else
		{
		#ifdef  DEBUG_UART
			if  (ucPWR_AccStatus == 1)
			{
				LOG_ACC("PWR_GetACCPort:OFF");
			}
		#endif
			ucPWR_AccStatus = 0;
		}
	}
	ucPWR_AccPortLast = ucAccPort;
}

void PWR_GetBackUpPort(void)
{
	unsigned char ucBackUpPort;

	ucBackUpPort = PI_BACKUP;

	if  (ucPWR_BackUpPortLast == ucBackUpPort)
	{
		if  (ucBackUpPort == 1)
		{
			ucPWR_BackUpStatus = 1;
		}
		else
		{
			if  (ucPWR_BackUpStatus == 1)
			{
				ucPWR_BackUp_Timer5msec = PWR_BACKUP_DET_TIME;//0;
			}
			ucPWR_BackUpStatus = 0;
		}
	}
	ucPWR_BackUpPortLast = ucBackUpPort;
}

void PWR_DisplayDriverOn(void)
{
#ifdef  PT6524
	PT6524_Initialize();
#endif
#ifdef  ST7565P
	ST7565P_DisplayOn();
#endif
}

void PWR_DisplayDriverOff(void)
{
#ifdef  PT6524
	PT6524_DisplayOff();
#endif
#ifdef  ST7565P
	ST7565P_DisplayOff();
#endif
	u2mr = 0x00;	// Serial I/O mode select bit[0,1,2] : (001) Clock synchronous serial I/O mode
	u2c0 = 0x00;	// UiBRG count source select bit [0,1] : (01) f8SIO is selected
					// Transmit register empty flag[3] : (1) No data present in transmit register(transmission completed)
					// Transfer format select bit[7] : (1) MSB First
	u2brg = 0;
	u2c1 = 0x00;
	u2smr2 = 0x00;
	u2smr3 = 0x00;
	u2smr4 = 0x00;	
	te_u2c1 = 0;
	s2tic = 0;
}

void PWR_BackupMain(unsigned char ucForce)
{
	unsigned short i;

#if  1
	if  ((PI_BACKUP == 1) && (ucForce == 0))
	{
		return;
	}

	PWR_PowerControl(PWR_ALL, 0);
	PO_ACC = 0;
	PO_STBY = 1;

	LOG_ACC("<<BACK UP>>");
	PO_MUTE = 1;
	
#ifdef  LC786XXX
	LC786XXX_CloseCommunication();
	LC786XXX_StopProcess();
	PO_LC786_RST = 0;
#endif

	TUNER_PowerOff();
#ifdef  HERO
	ucHERO_BootSeq = HERO_BOOT_SEQ_STOP;
#endif
#ifdef  ATOMIC
	ucATOMIC_BootSeq = ATOMIC_SEQ_STOP;
#endif
	PWR_DisplayDriverOff();
	DISP_ClearTempDisplayMode();
	MENU_Clear();
	USB_ScanOff();
	USB_RandomOff();
	USB_PauseOff(0);
#ifdef  CD
	CD_PauseOff(0);
	CD_ScanOff();
	CD_RandomOff();
#endif

	while(1)
	{	
		asm("fclr I");	/* Interrupt disabled */
	///////////////// CPU Function Disable ////////////////
		ta0s = 0;
#if  defined(M16C_64G) && defined(LC786XXX)
		s4ic = 0x00;
#endif
#ifdef  M16C_64G
		u0bcnic = 0x00;
#endif
#if  defined(ST7565P) || defined(PT6524)
		s2tic = 0x00;
#endif
		u0mr = 0;
		u2mr = 0;
#ifdef  M16C_64G
		u6mr = 0;
		u7mr = 0;
		prcr = 0x04;
		s4c = 0;
		prcr = 0x00;
#endif
		pd10 = 0x00;
		prcr = 0x04;		
		pd9 = 0x00;	// BIN_0111_1111
		prcr = 0x00;
		pd8 = 0x00;	// BIN_0000_0001
		pd7 = 0x00;	// BIN_0111_1101
		pd6 = pd6 & 0xF0;	// E8A
		pd5 = pd5 & 0x21;	// E8A
		pd4 = 0x00;	// BIN_1110_0100
		pd3 = 0x00;
		pd2 = 0x03;	// BIN_0000_0011
		pd1 = 0x00;	// BIN_1000_1011
		pd0 = 0x00; // BIN_1010_0000
				
		if	(OPT_GetOption(OPT_MASK_CLOCK))
		{
			prcr = 0x01;		
			cm03 = 0;		// XCIN-XCOUT drive capacity select bit : Low
			cm15 = 0;
			cm07 = 1;		// System clock select bit : Sub clock
			cm05 = 1;		// Main clock stop bit : Off
			prcr = 0x00;
		}
		else
		{
			prcr = 0x01;
			cm03 = 0;		// XCIN-XCOUT drive capacity select bit : Low
			cm15 = 1;		// XIN-XOUT Drive Capacity Select Bit : Low
	
	//		cm05 = 1;		// Main clock stop bit : Off
			prcr = 0x00;
		}

		int1ic = 0x14;
		asm("fset I");	/* Interrupt enabled */

		ucPWR_WakeUpMode = 0;
		while(1)
		{		
			if  ((ucPWR_WakeUpMode & PWR_WAKE_BACKUP) == PWR_WAKE_BACKUP)
			{
				ucPWR_WakeUpMode = 0;
				break;
			}
			RTC_CountUp();
			_asm("wait");	// Wait Mode
			_asm("nop");
		}

	//	asm("fclr I");	/* Interrupt disabled */
	//	int1ic = 0x00;
	//	asm("fset I");	/* Interrupt enabled */

		CPU_Initialize();
		
		if	(ucPWR_WakeUpMode == 0)
		{
			int1ic = 0x00;
			break;
		}
	}

	PO_STBY = 1;
	ucPWR_WakeUpMode = 0;
//	PWR_AccWakeUp();
	LOG_ACC("BACK UP:1");
//	ucPWR_ACC_Sequence = PWR_ACC_IDLE;
	ucPWR_ACC_Sequence = PWR_ACC_WAKEUP;
#ifdef  CD
	DECK_SetKeepWorking();
#endif
	usPWR_AccSequence_Timer5msec = 600/5;//50/5;	// 50/5;
#endif
}

#ifdef  CD
unsigned char PWR_CD_Eject(void)
{
	PO_ACC = 1;
	PO_LC786_RST = 1;
	PO_PWR1 = 1;
	CD_DRV_MUTE_OFF;

	if  (ucDECK_Disc == 0)
	{
		// Long Key Check
		unsigned short i = 800/5;
		
		while(1)
		{
			while(ucTask_Timer5msec)
			{
			};
			ucTask_Timer5msec = 1;
			if  (PI_EJECT_KEY == 1)
			{
				return(0);
			}

			--i;
			if  (i == 0)
			{
				break;
			}
		}
	}
	
	DECK_SetMode(DECK_MODE_EJECT);
	if  ((FUNC_GetCurrentFunction() == FUNC_CD) && (ucDECK_Disc == 1))
	{
		ucFUNC_Current = FUNC_GetPreviousFunction();
	}
	while(1)
	{
		while(ucTask_Timer5msec)
		{
		};
		ucTask_Timer5msec = 1;
		DECK_main();
		PWR_GetACCPort();

		if  (ucPWR_AccStatus == 1)
		{
			PO_LC786_RST = 0;
			return(1);
		}
		if  (DECK_CurrentWorking() == 0)
		{
			DECK_SetIdle();
			break;
		}
	}
	
	PO_ACC = 0;
	PWR_PowerControl(PWR_ALL, 0);
	PO_LC786_RST = 0;
	return(0);
}
#endif

#ifdef  DEBUG_UART
extern unsigned char UART6_BufferCnt(void);
#endif

void PWR_CPU_Sleep(void)
{
	unsigned char i;

#if	0//def  DEBUG_UART
	while(1)
	{
		if  (UART6_BufferCnt() == 0)
		{
			break;
		}
	}
#endif
#ifdef  LC786XXX
	LC786XXX_CloseCommunication();
	LC786XXX_StopProcess();
	PO_LC786_RST = 0;
#endif
	
	while(1)
	{
		asm("fclr I");	/* Interrupt disabled */
	///////////////// CPU Function Disable ////////////////
		ta0s = 0;
#if  defined(M16C_64G) && defined(LC786XXX)
		s4ic = 0x00;
#endif
#ifdef  M16C_64G
		u0bcnic = 0x00;
#endif
#if  defined(ST7565P) || defined(PT6524)
		s2tic = 0x00;
#endif
		PO_STBY = 0;

		u0mr = 0;
		u2mr = 0;
	#ifdef  M16C_64G
		u6mr = 0;
		u7mr = 0;
		prcr = 0x04;
		s4c = 0;
		prcr = 0x00;
	#endif
		pd10 = 0x00;
		prcr = 0x04;		
		pd9 = 0x00;	// BIN_0111_1111
		prcr = 0x00;
		pd8 = 0x00;	// BIN_0000_0001
		pd7 = 0x00;	// BIN_0111_1101
		pd6 = pd6 & 0xF0;	// E8A
		pd5 = pd5 & 0x21;	// E8A
		pd4 = 0x00;	// BIN_1110_0100
		pd3 = 0x00;
		pd2 = 0x03;	// BIN_0000_0011
		pd1 = 0x00;//0x00;	// BIN_1000_1011
		pd0 = 0x00; // BIN_1010_0000

		if  (OPT_GetOption(OPT_MASK_CLOCK))
		{
			prcr = 0x01;		
			cm03 = 0;		// XCIN-XCOUT drive capacity select bit : Low
			cm15 = 0;
			cm07 = 1;		// System clock select bit : Sub clock
			cm05 = 1;		// Main clock stop bit : Off
			prcr = 0x00;
		}
		else
		{
			prcr = 0x01;
			cm03 = 0;		// XCIN-XCOUT drive capacity select bit : Low
			cm15 = 1;		// XIN-XOUT Drive Capacity Select Bit : Low

	//		cm05 = 1;		// Main clock stop bit : Off
			prcr = 0x00;
		}

		int2ic = 0x13;	// INT2 Level : 3
						// Polarity select bit : (1) rising edge
		asm("fset I");	/* Interrupt enabled */

		ucPWR_WakeUpMode = 0;
		while(1)
		{
			if  (ucPWR_WakeUpMode != 0)	
			{
				break;
			}
			RTC_CountUp();
			_asm("wait");	// Wait Mode
			_asm("nop");
		}

		asm("fclr I");	/* Interrupt disabled */		
		int2ic = 0x00;
		int1ic = 0x00;

//		while(1)
//		{
//			CPU_ADC_GetBatteryLevel();
//			if	(usCPU_ADC_Battery > BAT_WAKEUP_LEV)
//			{
//				break;
//			}
//		}

		CPU_Initialize();
		asm("fset I");	/* Interrupt enabled */
		
		if  ((ucPWR_WakeUpMode & PWR_WAKE_ACC) == PWR_WAKE_ACC)
		{
			break;
		}
	#ifdef  CD
		if  (ucPWR_WakeUpMode == PWR_WAKE_EJECT)		// Eject Only
		{
			if  (PWR_CD_Eject() == 1)
			{
				break;
			}
		}
	#endif
	}
}

void PWR_LastFunction(void)
{
	LOG_ACC("PWR_LastFunction");
	if  (FUNC_PowerStatus()== 1)
	{
		LOG_ACC("ON");
	#ifdef  BLUETOOTH
		if  (ucFUNC_Current == FUNC_A2DP)
		{
			FUNC_FunctionChangeStart(FUNC_A2DP,FUNC_POWER_ON_SEQ0);
		}
		else
	#endif
		{
			if  (FUNC_CheckFunctionAvailable(FUNC_GetCurrentFunction()) == 1)
			{
			#ifdef  BLUETOOTH
				if  (FUNC_GetCurrentFunction() == FUNC_LPB)
				{
					FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_POWER_ON_SEQ0);
				}
			#endif
				{
					FUNC_FunctionChangeStart(FUNC_GetCurrentFunction(),FUNC_POWER_ON_SEQ0);
				}
			}
			else
			{
				FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_POWER_ON_SEQ0);
			}
		}
		ucFUNC_Power = 0;
	}
#ifdef  BLUETOOTH
	if  (ucPWR_BT_ConnectResume == 1)
	{
		BT_AutoConnectionStart();
		LOG_ACC("BTC");
	}
#endif
}

void PWR_AccWakeUp(void)
{
	PWR_PowerControl(PWR_CTR1, 1);
//	PWR_PowerControl(PWR_USB, 1);
	PO_ACC = 1;
//	PO_STBY = 1;
	Device_InitializeStart();
	PWR_LastFunction();
}
unsigned char PWR_CheckDeviceStatus(void)
{
//	LOG_ACC("PWR_DvcSt");
	
#ifdef  BLUETOOTH
	if  (BT_ConnectStatus() == 1)
	{
//		LOG_ACC("BT");
		return(0);
	}
#endif
#ifdef  CD
	if  (OPT_GetOption(OPT_MASK_CD))
	{
		if  (ucDECK_Mode != DECK_MODE_NONE)
		{
	//		LOG_ACC("DK");
			return(0);
		}
		if  (LC786XXX_GetMediaStatus(FUNC_CD) > LC786X_MS_STOP)
		{
	//		LOG_ACC("SY");
			return(0);
		}
	}
#endif
	return(1);
}

#ifdef  ST7565P
extern void ST7565_DisplayOff(void);
#endif

void PWR_BatteryMain(void)
{
	CPU_ADC_GetBatteryLevel();

	if  ((ucPWR_ACC_Sequence == PWR_ACC_IDLE) && (ucFUNC_Power == 1))
	{
		if  (usCPU_ADC_Battery < BATTERY_18V)
		{
			AUDIO_SetForceMute(1);
		}
		else
		{
			AUDIO_SetForceMute(0);
		}
	}

	if  (usCPU_ADC_Battery > BAT_SLEEP_LEV)
	{
		return;
	}
	
	while(1)
	{
		CPU_ADC_GetBatteryLevel();
		if  (usCPU_ADC_Battery > BAT_WAKEUP_LEV)
		{
			break;
		}
		PWR_BackupMain(1);
	}
	
	LOG_ACC("PWR_BatteryMain");
}

unsigned char PWR_ProcessIsWakeUp(void)
{
	if  (ucPWR_ACC_Sequence == PWR_ACC_WAKEUP)
	{
		return(1);
	}
	return(0);
}

unsigned char PWR_ProcessIsIdle(void)
{
	if  (ucPWR_ACC_Sequence == PWR_ACC_IDLE)
	{
		return(1);
	}
	return(0);
}


void PWR_Main(void)
{
	unsigned short i = 0;
	
	PWR_GetBackUpPort();
	PWR_BatteryMain();
	PWR_BackupMain(0);	// Not Need
	PWR_GetACCPort();
#if  1
	switch  (ucPWR_ACC_Sequence)
	{
		case PWR_ACC_IDLE:
			if  (ucPWR_AccStatus == 0)
			{
				LOG_ACC("PWR_ACC_MONITOR");
				DISP_ClearTempDisplayMode();
				MENU_Clear();
				AUDIO_MuteOn();
				PWR_PowerControl(PWR_BACKLIGHT, 0);
				PO_STBY = 0;
			#ifdef  BLUETOOTH
				ucPWR_BT_ConnectResume = 0;
				ucPWR_A2DP_Resume = 0;
				if  (BT_ConnectStatus() == 1)
				{
					BT_DisconnectRequest();
					ucPWR_BT_ConnectResume = 1;
				}
				if  (ucBT_AutoConnection == 1)
				{
					ucPWR_BT_ConnectResume = 1;
				}
				if  (FUNC_GetCurrentFunction() == FUNC_A2DP)
				{
					ucPWR_A2DP_Resume = 1;
				}
				BT_AutoConnectionStop();
			#endif
				USB_ScanOff();
				USB_RandomOff();
				USB_PauseOff(0);
			#ifdef  CD
				CD_PauseOff(0);
				CD_ScanOff();
				CD_RandomOff();
			#endif
				ucPWR_ACC_Sequence = PWR_ACC_REQ_DEVICE_OFF;
				usPWR_AccSequence_Timer5msec = 0;
			}
			break;
		case PWR_ACC_REQ_DEVICE_OFF:
			LOG_ACC("PWR_ACC_REQ_DEVICE_OFF");
			TUNER_PowerOff();
		#ifdef  HERO
			ucHERO_BootSeq = HERO_BOOT_SEQ_STOP;
		#endif
		#ifdef  ATOMIC
			ucATOMIC_BootSeq = ATOMIC_SEQ_STOP;
		#endif
			PWR_DisplayDriverOff();

			if  (ucLC786XXX_InitializeComplete == 1)
			{
				if  (FUNC_GetCurrentFunction() == FUNC_USB)
				{
					if  (LC786XXX_USB_GetPlayStatus() == 1)
					{
						LC786XXX_MediaStop(FUNC_USB);
					}
				}
			#ifdef CD
				if  (FUNC_GetCurrentFunction() == FUNC_CD)
				{
					if  (LC786XXX_GetMediaStatus(FUNC_CD) > LC786X_MS_STOP)
					{
						LC786XXX_MediaStop(FUNC_CD);
					}
				}
			#endif
			}

			ucPWR_ACC_Sequence = PWR_ACC_SLEEP;
			usPWR_AccSequence_Timer5msec = 5000/5;
			ucPWR_AccMute_Timer5msec = 20/5;
		#ifdef  CD
			if  (OPT_GetOption(OPT_MASK_CD))
			{
				if  (ucDECK_Mode != DECK_MODE_NONE)
				{
					ucPWR_ACC_Sequence = PWR_ACC_WAIT_DECK;
					usPWR_AccSequence_Timer5msec = 10000/5;
				}
			}
		#endif
			break;
		#ifdef  CD
		case PWR_ACC_WAIT_DECK:
			if  ((ucDECK_Mode == DECK_MODE_NONE) || (ucDECK_Mode == DECK_MODE_ERROR))
			{
				ucPWR_ACC_Sequence = PWR_ACC_SLEEP;
			}
			else
			{
				if  (ucPWR_AccStatus == 1)
				{
					ucPWR_ACC_Sequence = PWR_ACC_WAKEUP;
					PO_STBY = 1;
					usPWR_AccSequence_Timer5msec = 0;
				}
			}
			break;
		#endif
		case PWR_ACC_SLEEP:
			if  (ucPWR_AccMute_Timer5msec != 0)
			{
				break;
			}
			if  ((PWR_CheckDeviceStatus() == 1) || (usPWR_AccSequence_Timer5msec == 0))
			{
			#ifdef  LC786XXX
				LC786XXX_StopProcess();
				PO_LC786_RST = 0;
			#endif
			#ifdef  BLUETOOTH
				BT_FunctionStop();
			#endif
	//		#ifdef  CD
	//			DECK_InitailizeValue();
	//		#endif
				LOG_ACC("PWR_ACC_WAIT_DEVICE_OFF");
				PWR_PowerControl(PWR_ALL, 0);
				PO_ACC = 0;
			#ifdef  CD
				DECK_SetIdle();
			#endif
				if  (ucPWR_AccStatus == 0)
				{
					LOG_ACC("PWR_ACC_SLEEP");
					PWR_CPU_Sleep();
				}
				PO_STBY = 1;
				ucPWR_WakeUpMode = 0;
				for(i = 0; i < 10; ++i)
				{
					PWR_GetACCPort();
				}
				ucPWR_ACC_Sequence = PWR_ACC_WAKEUP;
				usPWR_AccSequence_Timer5msec = 600/5;//50/5;	// 50/5;
			#ifdef  CD
				DECK_SetKeepWorking();
			#endif
			}
			else
			{
			}
			break;
		case PWR_ACC_WAKEUP:
			if  (ucPWR_AccStatus == 1)
			{
				if  (usPWR_AccSequence_Timer5msec == 0)
				{
				#if  1
			//		while(1)
			//		{
			//			CPU_ADC_GetBatteryLevel();
			//			if  (usCPU_ADC_Battery > BAT_WAKEUP_LEV)
			//			{
			//				break;
			//			}
			//		}
			//		CPU_ADC_GetBatteryLevel();
			//		if  (usCPU_ADC_Battery > 600)
			//		{
			//			return;
			//		}
				#endif
					LOG_ACC("PWR_ACC_WAKEUP");
					PWR_AccWakeUp();
					ucPWR_ACC_Sequence = PWR_ACC_IDLE;
				#ifdef  ZYLE_LOGO
					DISP_SetLogo();
				#endif
				}
			}
			else
			{
				ucPWR_ACC_Sequence = PWR_ACC_SLEEP;
				LOG_ACC("PWR_ACC_WAKEUP:SLEEP");
			}
			break;
	#ifdef  BLUETOOTH
		case PWR_ACC_WAIT_BT:
			if  (ucPWR_AccStatus == 0)
			{
				ucPWR_ACC_Sequence = PWR_ACC_SLEEP;
			}
			else
			{
				if  ((BT_HF_ConnectStatus() == 1) && (BT_A2DP_ConnectStatus() == 1))
				{
					ucPWR_ACC_Sequence = PWR_ACC_IDLE;
					ucPWR_BT_ConnectResume = 0;
					LOG_ACC("PWR_ACC_WAIT_BT");
				}
			}
			break;
	#endif
	}
#endif
	PWR_CheckAntennaStatus();
}



